import 'package:flutter/material.dart';
import 'dart:async';

import 'package:flutter/services.dart';
import 'package:u_push_plugin/u_push_plugin.dart';
import 'package:u_push_plugin/data.dart';
import 'package:u_push_plugin_example/ios_test.dart';

import 'test_message_data.dart';
//import 'package:u_push_plugin_example/test_message_data.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  String deviceToken = "";
  String message = "";

  @override
  void initState() {
    super.initState();
    UPushPlugin.initMessageListener();
    UPushPlugin.initRegisterListener((value) {
      deviceToken = value.message;
      setState(() {});
    });
    UPushPlugin.initMessageResultListener((value) {

      print("value is ${value}");

      MessageData(value);
      setState(() {
        message = MessageData(value).message;
      });
    });
    UPushPlugin.getCacheMessage().then((value) {
      if (value != null) {
        if (value.isNotEmpty) {
          setState(() {
            message = "99999 ${value.toString()}";
          });
        }
      }
    });
    UPushPlugin.initMessageReceiverListener((value) {
      message = value.toString();
      setState(() {});
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
//    String platformVersion;
//    if (!mounted) return;
//
//    setState(() {
//      _platformVersion = platformVersion;
//    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: ListView(
          children: [
            Text(deviceToken),
            Text(message),
            FlatButton(
                onPressed: () async {
                  await UPushPlugin.UMInitIOS("5fd0448dfb95730a9f82dd0e");
                  setState(() {});
                },
                child: Text("初始化appkey")),
            FlatButton(
                onPressed: () async {
                  deviceToken = await UPushPlugin.getDeviceToken();
                  setState(() {});
                },
                child: Text("获取登录信息")),
            FlatButton(
                onPressed: () async {
                  var data = await UPushPlugin.getTags();
//                  setState(() {});
                },
                child: Text("获取签名")),
            FlatButton(
                onPressed: () async {
                  print("添加签名");
                  var data = await UPushPlugin.addTags(["1", "2", "3"]);
                  print(data);

//                  setState(() {});
                },
                child: Text("添加标签")),
            FlatButton(
                onPressed: () async {
                  print("删除标签");
                  var data = await UPushPlugin.deleteTags(["1", "2", "3"]);
                  print(data);
//                  setState(() {});
                },
                child: Text("删除标签")),
            FlatButton(
                onPressed: () async {
                  print("添加别名");
                  var data = await UPushPlugin.addAlias("testPush", "yyt");
                  print("添加别名 is $data");


                  iOSTestData test = iOSTestData(data["data"]);
                  message = test.message;
                  print("$message");
                  setState(() {});
                },
                child: Text("添加别名")),
            FlatButton(
                onPressed: () async {
                  print("设置别名");
                  var data = await UPushPlugin.setAlias("testPush", "yyt");
                  print(data);
//                  setState(() {});
                },
                child: Text("设置别名")),
            FlatButton(
                onPressed: () async {
                  print("删除别名");
                  var data = await UPushPlugin.deleteAlias("testPush", "yyt");
                  print(data);
//                  setState(() {});
                },
                child: Text("删除别名")),
            FlatButton(
                onPressed: () async {
                  print("开启原生");
                  UPushPlugin.UMSetLogEnabled(true);
//                  setState(() {});
                },
                child: Text("开启原生")),
            FlatButton(
                onPressed: () async {
                  print("关闭原生");
                  UPushPlugin.UMSetLogEnabled(false);
//                  setState(() {});
                },
                child: Text("关闭原生")),
            FlatButton(
                onPressed: () async {
                  print("开启flutter");
                  UPushPlugin.changeFlutterLog(true);
//                  setState(() {});
                },
                child: Text("开启flutter")),
            FlatButton(
                onPressed: () async {
                  print("关闭flutter");
                  UPushPlugin.changeFlutterLog(false);
//                  setState(() {});
                },
                child: Text("关闭flutter")),
            FlatButton(
                onPressed: () async {
                  print("获取推送消息");
                  UPushPlugin.getCacheMessage().then((value) {
                    if (value != null) {
                      if (value.isNotEmpty) {
                        setState(() {
                          print('dddddd:$value');
                          message = MessageData(value).message;
                          iOSTestData data = iOSTestData(value);

                        });
                      }
                    }
                  });
//                  setState(() {});
                },
                child: Text("获取推送消息")),
          ],
        ),
//        Column(
//          children: <Widget>[
//            Text(deviceToken),
//            Text(message),
//            FlatButton(
//                onPressed: () async {
//                  await UPushPlugin.UMInitIOS("5fd0448dfb95730a9f82dd0e");
//                  setState(() {});
//                },
//                child: Text("初始化appkey")),
//            FlatButton(
//                onPressed: () async {
//                  deviceToken = await UPushPlugin.getDeviceToken();
//                  setState(() {});
//                },
//                child: Text("获取登录信息")),
//            FlatButton(
//                onPressed: () async {
//                  var data = await UPushPlugin.getTags();
////                  setState(() {});
//                },
//                child: Text("获取签名")),
//            FlatButton(
//                onPressed: () async {
//                  print("添加签名");
//                  var data = await UPushPlugin.addTags(["1", "2", "3"]);
//                  print(data);
////                  setState(() {});
//                },
//                child: Text("添加标签")),
//            FlatButton(
//                onPressed: () async {
//                  print("删除标签");
//                  var data = await UPushPlugin.deleteTags(["1", "2", "3"]);
//                  print(data);
////                  setState(() {});
//                },
//                child: Text("删除标签")),
//            FlatButton(
//                onPressed: () async {
//                  print("添加别名");
//                  var data = await UPushPlugin.addAlias("testPush", "yyt");
//                  print(data);
////                  setState(() {});
//                },
//                child: Text("添加别名")),
//            FlatButton(
//                onPressed: () async {
//                  print("设置别名");
//                  var data = await UPushPlugin.setAlias("testPush", "yyt");
//                  print(data);
////                  setState(() {});
//                },
//                child: Text("设置别名")),
//            FlatButton(
//                onPressed: () async {
//                  print("删除别名");
//                  var data = await UPushPlugin.deleteAlias("testPush", "yyt");
//                  print(data);
////                  setState(() {});
//                },
//                child: Text("删除别名")),
//            FlatButton(
//                onPressed: () async {
//                  print("开启原生");
//                  UPushPlugin.UMSetLogEnabled(true);
////                  setState(() {});
//                },
//                child: Text("开启原生")),
//            FlatButton(
//                onPressed: () async {
//                  print("关闭原生");
//                  UPushPlugin.UMSetLogEnabled(false);
////                  setState(() {});
//                },
//                child: Text("关闭原生")),
//            FlatButton(
//                onPressed: () async {
//                  print("开启flutter");
//                  UPushPlugin.changeFlutterLog(true);
////                  setState(() {});
//                },
//                child: Text("开启flutter")),
////            FlatButton(
////                onPressed: () async {
////                  print("关闭flutter");
////                  UPushPlugin.changeFlutterLog(false);
//////                  setState(() {});
////                },
////                child: Text("关闭flutter"))
//          ],
//        ),
      ),
    );
  }
}
